package com.shelpe.services.algorithm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.algorithm.client.SinbadClient;
import com.shelpe.services.algorithm.entity.jpa.Adgroup;
import com.shelpe.services.algorithm.mobileQscore.MobileQscoreSustainRule;
import com.shelpe.services.algorithm.mobileQscore.MobileQscoreSustainer;
import com.shelpe.services.algorithm.repository.jpa.AdgroupRepository;
import com.shelpe.services.algorithm.sustain.SustainRule;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/algorithmservice")
@Slf4j
public class MobileQscoreController {
	@Autowired
	private AdgroupRepository adgroupRepository;
	@Autowired
	private MobileQscoreSustainRule sustainRule;
	@Autowired
	private SinbadClient sinbadClient;

	@RequestMapping(path = "/users/{user_id}/mobileQscores/@execute", method = RequestMethod.GET)
	@ResponseBody
	public ExecutionRes executeMobileQscore(@RequestParam("adgroup_id") long adgroupID) {
		log.info("begin executeMobileQscore {} {}", adgroupID);
		Adgroup adgroup = this.adgroupRepository.findOne(adgroupID);
		Assert.notNull(adgroup, "adgroup_not_found");
		Assert.isTrue(adgroup.getDriveType() == 2, "is_not_mobile_qscore");

		log.debug("adgroup: {}", adgroup);
		ExecutionRes dto = new ExecutionRes();
		dto.setCampaignID(adgroup.getCampaignID());
		dto.setProductID(adgroup.getProductID());
		dto.setAdgroupID(adgroup.getAdgroupID());

		MobileQscoreSustainer sustainer = new MobileQscoreSustainer();
		sustainer.setAdgroup(adgroup);
		String error = "";
		boolean toNext = this.sustainRule.check(sustainer);
		if (toNext) {
			toNext = this.sustainRule.prepare(sustainer);
			if (toNext) {
				SustainRule.SustainStatus ss = this.sustainRule.sustain(sustainer);
				toNext = this.sustainRule.maintain(sustainer, ss);
				if (toNext) {
					toNext = this.sustainRule.mark(sustainer);
					if (!toNext) {
						error = "fail_to_mark";
					}
				} else {
					error = "fail_to_maintain";
				}
			} else {
				error = "fail_to_prepare";
			}
		} else {
			error = "fail_to_check";
		}
		dto.setSuccess(toNext);
		if (!toNext) {
			dto.setMessage(error);
		}
		return dto;
	}

	@Data
	@JsonInclude(Include.NON_NULL)
	public static class ExecutionRes {
		private boolean success;

		@JsonProperty("campaign_id")
		private long campaignID;
		@JsonProperty("product_id")
		private long productID;
		@JsonProperty("default_price")
		private int defaultPrice;
		@JsonProperty("adgroup_id")
		private long adgroupID;

		private String message;
	}

}
